package com.mrxu.monitor.service;

import cn.hutool.core.util.ObjectUtil;
import com.mrxu.monitor.config.MonitorProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.ArrayList;
import java.util.List;

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    @Autowired
    private MonitorRequestInterceptor monitorRequestInterceptor;

    @Autowired
    private MonitorProperties monitorProperties;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        if(monitorProperties.isEnable()) {
            InterceptorRegistration registration = registry.addInterceptor(monitorRequestInterceptor);
            if(monitorProperties.getInterceptor().getPath() != null
                    && !monitorProperties.getInterceptor().getPath().isEmpty()) {
                registration.addPathPatterns(monitorProperties.getInterceptor().getPath());
            }
            List<String> excludePath = new ArrayList<>();
            excludePath.add("/monitor/**");
            if(monitorProperties.getInterceptor().getExcludePath() != null
                    && !monitorProperties.getInterceptor().getExcludePath().isEmpty()) {
                excludePath.addAll(monitorProperties.getInterceptor().getExcludePath());
            }
            registration.excludePathPatterns(excludePath);
        }
    }

}
